var express = require('express');
var router = express.Router();
const orderService = require('../service/orderService');
const productTypeService = require('../service/productTypeService');

// 按季度统计个类型商品销售额
router.get('/season',async function(req,res){
    let xAxisData = await productTypeService.findAll();
    xAxisData = xAxisData.map(item => item.name);
    let tempData = await orderService.findAll(req.query.year);
    let seasonData = {};
    tempData.forEach((item) => {
        let date = new Date(item.createTime);
        if(date.getMonth() < 3){
            seasonData["一季度"] = [...(seasonData["一季度"] || []),...item.products];
        }
        else if(date.getMonth() < 6){
            seasonData["二季度"] = [...(seasonData["二季度"] || []),...item.products];
        }
        else if(date.getMonth() < 9){
            seasonData["三季度"] = [...(seasonData["三季度"] || []),...item.products];
        }
        else if(date.getMonth() < 12){
            seasonData["四季度"] = [...(seasonData["四季度"] || []),...item.products];
        }
       
    });
     let series = Object.keys(seasonData).map(key => {
        let seriesData = {name:key,type:'bar',data:new Array(xAxisData.length).fill(0)};
        seasonData[key].forEach((product) => {
            xAxisData.forEach((productType,i) => {
                if(product.type == productType){
                    seriesData.data[i] += product.price
                }
            })
            
        });
        return seriesData;
    })
   
   
   console.log(series);
    res.send({xAxisData,series});
});
router.get('/area',async function(req,res){
    let xAxisData = await productTypeService.findAll();
    let orderData = await orderService.findAll(req.query.year);
    let seriesData = xAxisData.map(item => {
        let data = {
            name:item.name,
            value:0
        }
        orderData.forEach(order => {
            order.products.forEach(product => {
                if(product.type == item.name){
                    data.value += product.price
                }
            });
        })
        return data;

    })
    console.log(seriesData);
    
    res.send(seriesData);
});


module.exports = router;